home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15228 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.2 KB  |  137 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Return type - array of structures - please help!
  5. Date: 17 Apr 1996 13:05:17 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4l3ittINNho9@keats.ugrad.cs.ubc.ca>
  8. References: <4l0c9g$tsr@thorn.cc.usm.edu> <4l0em9$ubi@thorn.cc.usm.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4l0em9$ubi@thorn.cc.usm.edu>,
  12. Michael Kirgan <mkirgan@pacific.st.usm.edu> wrote:
  13. >Michael Kirgan (mkirgan@pacific.st.usm.edu) wrote:
  14. >
  15. >BTW, I forgot to mention something.  The errors I get are:  syntax errors
  16. >on the function prototype & function header.  The other error occured
  17. >on this statement class = delete(delssn, class);  and that error was:
  18. >operation between types   array & struct.  Class is an array of structures
  19. >& like I said before I am trying to make the return type the same.
  20.  
  21. Ah, but you said in the other posting that ``the above code works''. This
  22. cannot be said of any program that has syntax errors, since it will not
  23. translate.
  24.  
  25. In any case, here is your stuff again:
  26.  
  27.  >typdef struct
  28.  > {
  29.  >  char ssn[12];
  30.  >  char lastname[15];
  31.  >  char firstname[15];
  32.  >  char data[10];
  33.  > } student;
  34.  
  35. So far so good.
  36.  
  37.  >void delete(char [], class [])
  38.  
  39. Warning: array parameters in C are ``silently'' taken to mean pointers. The
  40. declaration you want is probably this:
  41.  
  42.     void delete(char ssn[], student class[])
  43.  
  44. The above declaration is identical to:
  45.  
  46.     void delete(char *ssn, student *class)
  47.  
  48. There is no way to pass wholesale arrays to, or from, a function in C.
  49. Structures can be passed and returned, however. If you desperately want to pass
  50. (fixed size) arrays, you can encapsulate them in structures.
  51.  
  52.  > {
  53.  >  student temp[25]; 
  54.  >  /*omitted the body.  All it does is delete a struct from the array*/
  55.  >  /*temp is the new array with the structure deleteted and it is copied
  56.  >  /*back into students*/
  57.  >  
  58.  >
  59.  >  class = temp;
  60.  > }
  61.  
  62. You would not write a function in C like the above. It would not be wise. What
  63. you want is to pass a pointer to the first element of the array, and also tell
  64. the function the _size_ of the array (the function has no way of knowing how
  65. big the array is unless you tell it to).
  66.  
  67. Since you are deleting a student, you want to pass a _pointer_ to the number of
  68. students, because you will be changing that number, decreasing it by one.
  69.  
  70. #include <string.h>
  71.  
  72. void delete_student(char *ssn, student *class, int *num)
  73.  
  74. {
  75.     int i;
  76.  
  77.     /* search for a matching ssn in array    */
  78.  
  79.     for (i = 0; i < *num; i++) 
  80.         if (!strcmp(class[i].ssn, ssn))    {    /* match!    */
  81.             --*num;            /* bump down size    */
  82.             class[i] = class[*num];    /* move last element in    */
  83.             break;            /* break out of loop    */
  84.         }
  85. }
  86.  
  87.  
  88.  >main()
  89.  > {
  90.  >  student class[25];
  91.  >  
  92.  >  /*there is now a loop to read in the class info.*/
  93.  >  /*next there is a loop to process options, such as printing out the
  94.  >    contents of the class, searching for a student in class, deleting,
  95.  >    & inserting*/
  96.  >
  97.  >  /*I was currently calling delete like this:*/
  98.  >  delete(delssn, class);
  99.  > }
  100.  
  101. You would have to change that to:
  102.  
  103. main()
  104. {
  105.     student class[25];
  106.     int clsize = 0;        /* class initially empty    */
  107.  
  108.     .
  109.     .    /* add some students to the class    */
  110.     .
  111.  
  112.     delete("1234", class, &clsize);    /* del student whose ssn is "1234" */
  113.  
  114.     return 0;
  115. }
  116.  
  117. >The above should have the same effect as when I said:  class = temp in the 
  118.  
  119. It is not possible to assign to an array variable in C. In an assignment like
  120.  
  121.     class = temp;
  122.  
  123. The right hand side expression produces a pointer to the first element of array
  124. temp, rather than an ``array rvalue'' (no such thing). The right side is an
  125. array lvalue, but it is not a modifiable lvalue. And it's not type compatible
  126. with the right side anyway...
  127.  
  128. You can forget about being able to assign arrays or move them around; you can
  129. only manipulate them as pointers.
  130.  
  131. About the only operator which treats an array as a fully fledged object is the
  132. sizeof operator, and the & operator (address of). If you have an array temp of
  133. 10 characters, then sizeof temp is 10, and &temp produces a pointer to an array
  134. of 10 char (char (*)[10]), not a pointer to char (char *).
  135. -- 
  136. I'm not really a jerk, but I play one on Usenet.
  137.